package cn.iocoder.yudao.module.vlog.comment.convert;

import cn.iocoder.yudao.framework.business.basic.pojo.vo.UserSimpleVO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.module.vlog.comment.bo.VLogCommentBO;
import cn.iocoder.yudao.module.vlog.comment.vo.AppVLogCommentRespVO;
import cn.iocoder.yudao.module.vlog.comment.vo.VLogCommentRespVO;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.factory.Mappers;

import java.util.List;
import java.util.Objects;

@Mapper
public interface VLogCommentConvert {

    VLogCommentConvert INSTANCE = Mappers.getMapper(VLogCommentConvert.class);

    @Mapping(target = "shopId", ignore = true)
    @Mapping(target = "toUser", ignore = true)
    @Mapping(target = "user", ignore = true)
    VLogCommentRespVO convert(VLogCommentBO bean);

    PageResult<VLogCommentRespVO> convertPage(PageResult<VLogCommentBO> page);

    default PageResult<VLogCommentRespVO> convertPage(PageResult<VLogCommentBO> pageResult,
                                                      List<UserSimpleVO> users) {
        PageResult<VLogCommentRespVO> page = convertPage(pageResult);
        page.getList().forEach(vo -> {
            UserSimpleVO user = users.stream().filter(u -> Objects.equals(u.getId(), vo.getUserId())).findFirst().orElse(null);
            vo.setUser(user);
            UserSimpleVO toUser = users.stream().filter(u -> Objects.equals(u.getId(), vo.getToUserId())).findFirst().orElse(null);
            vo.setToUser(toUser);
        });
        return page;
    }

    List<AppVLogCommentRespVO> convertList(List<VLogCommentBO> list);

    default List<AppVLogCommentRespVO> convertList(List<VLogCommentBO> list, List<UserSimpleVO> users) {
        List<AppVLogCommentRespVO> respVOS = convertList(list);
        respVOS.forEach(vo -> {
            UserSimpleVO user = users.stream().filter(u -> Objects.equals(u.getId(), vo.getUserId())).findFirst().orElse(null);
            vo.setUser(user);
            UserSimpleVO toUser = users.stream().filter(u -> Objects.equals(u.getId(), vo.getToUserId())).findFirst().orElse(null);
            vo.setToUser(toUser);
        });
        return respVOS;
    }

    PageResult<AppVLogCommentRespVO> convertAppPage(PageResult<VLogCommentRespVO> pageResult);

}
